﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SmartMobile.Data;
using SMARTMobile.Models;
using SMARTMobile.Models.Local;
using SMARTMobile.Models.UI;
using SMARTMobile.WinPhone;
using SMARTMobile.WinPhone.Models;

namespace SMARTMobile.ViewModels.Advance
{
    public class CurrentInDoorDoorDisplayViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<CurrentInDoorDisplayExtend> _currentInDoorDoorDisplayObservable;
        private ObservableCollection<CurrentOutDoorDisplayExtend> _currentOutDoorDisplayObservable;
        private ObservableCollection<PhonevenforVolumeExtend> _phonevendorVolumeObservable;
        private ObservableCollection<PhoneVendor> _phoneVenderObservable;
        private CurrentOutDoorDisplayExtend _selectedOutDoorDisplayObservable;
        #endregion

        public CurrentInDoorDoorDisplayViewModel()
        {
            this.CreateConnection();
            //this.GetPhoneVender();
            //this.GetPhonevendorVolume();
            this.GetCurrentInDoorDoorDisplay();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<CurrentInDoorDisplayExtend> CurrentInDoorDoorDisplayObservable
        {
            get { return this._currentInDoorDoorDisplayObservable; }
            set
            {
                this._currentInDoorDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentInDoorDoorDisplayObservable");
            }
        }

        public ObservableCollection<CurrentOutDoorDisplayExtend> CurrentOutDoorDisplayObservable
        {
            get { return this._currentOutDoorDisplayObservable; }
            set
            {
                this._currentOutDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentOutDoorDisplayObservable");
            }
        }

        public ObservableCollection<PhonevenforVolumeExtend> PhonevendorVolumeObservable
        {
            get { return this._phonevendorVolumeObservable; }
            set
            {
                this._phonevendorVolumeObservable = value;
                base.RaisePropertyChanged("PhonevendorVolumeObservable");
            }
        }

        public CurrentOutDoorDisplayExtend SelectedOutDoorDisplayObservable
        {
            get { return this._selectedOutDoorDisplayObservable; }
            set
            {
                this._selectedOutDoorDisplayObservable = value;
                base.RaisePropertyChanged("SelectedOutDoorDisplayObservable");
            }
        }

        public ObservableCollection<PhoneVendor> PhoneVendorObservable
        {
            get { return this._phoneVenderObservable; }
            set
            {
                this._phoneVenderObservable = value;
                base.RaisePropertyChanged("PhoneVenderObservable");
            }
        }

        public List<InDoorType> InDoorTypeList
        {
            get
            {
                var inDoorList = this._smartMobileContext.InDoorType.ToList();
                return inDoorList;
            }
        }

        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<CurrentInDoorDoorDisplay> list = this._smartMobileContext.CurrentInDoorDoorDisplay.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                this._smartMobileContext.CurrentInDoorDoorDisplay.DeleteAllOnSubmit(list);

                this._smartMobileContext.SubmitChanges();
            }

            // Save current in door
            List<CurrentInDoorDoorDisplay> currentInList = new List<CurrentInDoorDoorDisplay>();
            foreach (var item in this.CurrentInDoorDoorDisplayObservable)
            {
                if (GlobalInformation.SelectedTCARCUS != null)
                {
                    CurrentInDoorDoorDisplay outDoor = new CurrentInDoorDoorDisplay
                    {
                        ID_PK = Guid.NewGuid(),
                        CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        InDoorTypeCode = item.InDoorTypeCode,
                        Qty = item.Qty,
                        PictureURL = item.PictureURL,
                        QtyPicture = item.QtyPicture,
                        WherePicture = item.WherePicture
                    };

                    currentInList.Add(outDoor);
                }
            }

            if (currentInList.Any())
            {
                this._smartMobileContext.CurrentInDoorDoorDisplay.InsertAllOnSubmit(currentInList);
                this._smartMobileContext.SubmitChanges();
            }

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method
        private void GetPhonevendorVolume()
        {
            this.PhonevendorVolumeObservable = new ObservableCollection<PhonevenforVolumeExtend>();

            foreach (var item in this.PhoneVendorObservable)
            {
                PhonevenforVolumeExtend volume = new PhonevenforVolumeExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    PhoneVendorName = item.PhoneVendorName,
                    RetailVolume = 0,
                    RetailValue = 0,
                    WhosesaleValue = 0,
                    WhosesaleVolume = 0
                };

                this.PhonevendorVolumeObservable.Add(volume);
            }
        }

        private void GetCurrentInDoorDoorDisplay()
        {
            this.CurrentInDoorDoorDisplayObservable = new ObservableCollection<CurrentInDoorDisplayExtend>();

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<CurrentInDoorDoorDisplay> priceRangeContributions = this._smartMobileContext.CurrentInDoorDoorDisplay.Where(p => p.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (priceRangeContributions.Count > 0)
                {
                    ////foreach (var item in this.PhoneVendorObservable)
                    ////{
                    ////    priceRangeContributions.FirstOrDefault(
                    ////        a => a.PhoneVendorCode == item.PhoneVendorCode && a.InDoorTypeCode == item.);
                    ////}

                    foreach (var item in priceRangeContributions)
                    {
                        CurrentInDoorDisplayExtend currentIn = new CurrentInDoorDisplayExtend
                        {
                            ID_PK = item.ID_PK,
                            CustomerKey = item.CustomerKey,
                            PhoneVendorCode = item.PhoneVendorCode,
                            InDoorTypeCode = item.InDoorTypeCode,
                            PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode),
                            InDoorTypeName = this.GetIndoorTypeName(item.InDoorTypeCode),
                            Qty = item.Qty,
                            QtyPicture = item.QtyPicture,
                            WherePicture = item.WherePicture
                        };
                        this.CurrentInDoorDoorDisplayObservable.Add(currentIn);
                    }
                }
                else
                {
                    CreateList();
                }
            }

            if (GlobalInformation.SelectedTCARCUS == null)
            {
                CreateList();
            }
        }

        public void CreateList()
        {
            List<PhoneVendor> phonevendors = this._smartMobileContext.PhoneVendor.ToList();

            foreach (var item in phonevendors)
            {
                foreach (var indoorType in this.InDoorTypeList)
                {
                    CurrentInDoorDisplayExtend currentIn = new CurrentInDoorDisplayExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        InDoorTypeCode = indoorType.InDoorTypeCode,
                        PhoneVendorName = item.PhoneVendorName,
                        InDoorTypeName = indoorType.Description,
                        QtyPicture = indoorType.NumberPicture,
                        WherePicture = string.Empty
                    };
                    this.CurrentInDoorDoorDisplayObservable.Add(currentIn);
                }
            }

            // Save current in door
            List<CurrentInDoorDoorDisplay> currentInList = new List<CurrentInDoorDoorDisplay>();
            foreach (var item in this.CurrentInDoorDoorDisplayObservable)
            {
                CurrentInDoorDoorDisplay outDoor = new CurrentInDoorDoorDisplay
                {
                    ID_PK = item.ID_PK,
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    InDoorTypeCode = item.InDoorTypeCode,
                    Qty = item.Qty,
                    QtyPicture = item.QtyPicture,
                    PictureURL = item.PictureURL,
                    WherePicture = item.WherePicture
                };

                currentInList.Add(outDoor);
            }

            if (currentInList.Any())
            {
                this._smartMobileContext.CurrentInDoorDoorDisplay.InsertAllOnSubmit(currentInList);
                this._smartMobileContext.SubmitChanges();
            }
        }

        private void GetPhoneVender()
        {
            this.PhoneVendorObservable = new ObservableCollection<PhoneVendor>(this._smartMobileContext.PhoneVendor);
        }

        private string GetPhoneVendorName(string phonevendorCode)
        {
            return this._smartMobileContext.PhoneVendor.Where(p => p.PhoneVendorCode == phonevendorCode).Select(p => p.PhoneVendorName).FirstOrDefault();
        }

        private string GetIndoorTypeName(string indoorTypeCode)
        {
            return this._smartMobileContext.InDoorType.Where(p => p.InDoorTypeCode == indoorTypeCode).Select(p => p.Description).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        public void SavePreviousAdvanceType()
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<CurrentInDoorDoorDisplay> list = this._smartMobileContext.CurrentInDoorDoorDisplay.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                this._smartMobileContext.CurrentInDoorDoorDisplay.DeleteAllOnSubmit(list);
                this._smartMobileContext.SubmitChanges();
            }

            // Save current in door
            List<CurrentInDoorDoorDisplay> currentInList = new List<CurrentInDoorDoorDisplay>();
            foreach (var item in this.CurrentInDoorDoorDisplayObservable)
            {
                if (GlobalInformation.SelectedTCARCUS != null)
                {
                    CurrentInDoorDoorDisplay outDoor = new CurrentInDoorDoorDisplay
                    {
                        ID_PK = Guid.NewGuid(),
                        CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        InDoorTypeCode = item.InDoorTypeCode,
                        Qty = item.Qty,
                        QtyPicture = item.QtyPicture,
                        PictureURL = item.PictureURL,
                        WherePicture = item.WherePicture
                    };

                    currentInList.Add(outDoor);
                }
            }

            if (currentInList.Any())
            {
                this._smartMobileContext.CurrentInDoorDoorDisplay.InsertAllOnSubmit(currentInList);
                this._smartMobileContext.SubmitChanges();
            }
            //this.SaveCurrentInDoorDisplay();
        }

        public void SaveCurrentInDoorDisplay()
        {
            this._smartMobileContext.CurrentInDoorDoorDisplay.DeleteAllOnSubmit(this._smartMobileContext.CurrentInDoorDoorDisplay.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());

            this._smartMobileContext.SubmitChanges();

            // Save current in door
            List<CurrentInDoorDoorDisplay> currentInList = new List<CurrentInDoorDoorDisplay>();
            foreach (var item in this.CurrentInDoorDoorDisplayObservable)
            {
                //if (!string.IsNullOrEmpty(item.PictureURL))
                //{
                CurrentInDoorDoorDisplay outDoor = new CurrentInDoorDoorDisplay
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    InDoorTypeCode = item.InDoorTypeCode,
                    Qty = item.Qty,
                    QtyPicture = item.QtyPicture,
                    PictureURL = item.PictureURL,
                    WherePicture = item.WherePicture
                };

                currentInList.Add(outDoor);
                //}
            }

            if (currentInList.Any())
            {
                this._smartMobileContext.CurrentInDoorDoorDisplay.InsertAllOnSubmit(currentInList);
            }

            this._smartMobileContext.SubmitChanges();
        }

        public void UpdateData(string inDoorTypeCode, string phoneVendorCode, int qty)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                var indoorDisplay = (from outdoor in this._smartMobileContext.CurrentInDoorDoorDisplay
                                      where
                                          outdoor.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey &&
                                          outdoor.InDoorTypeCode == inDoorTypeCode && outdoor.PhoneVendorCode == phoneVendorCode
                                      select outdoor).FirstOrDefault();

                if (indoorDisplay != null)
                {
                    indoorDisplay.Qty = qty;
                    this._smartMobileContext.SubmitChanges();
                }
            }
        }

        #endregion
    }
}
